package questions;

import resource.Resource;
import utils.DateUtils;

public class Entry implements Comparable<Entry>
{
	
	private String section;
	private String type;
	private String question;
	private int page;
	private int priority;
	private long date;

	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public long getDate() {
		return date;
	}

	public void setDate(long date) {
		this.date = date;
	}

	public Entry(String section, String type, String question, int page, int priority, long date)
	{
		this.section= section;
		this.question= question;
		this.type= type;
		this.page = page;
		
		this.priority = priority;
		this.date = date;
	}
	
	public Entry(String section, String question, String type, int page)
	{
		this.section= section;
		this.question= question;
		this.type= type;
		this.page = page;
	}
	
	public Entry(String section, String all)
	{
		this.section= section;
		
		String rest= all;
		
		//type
		String[] typeA = rest.split(":");
		//System.out.println(Arrays.toString(typeA));
		this.type = typeA[0];
		rest = rest.substring( (typeA[0].length()) + 2);
		//question
		String[] questionA = rest.split("[0-9]");
		//System.out.println(Arrays.toString(questionA));
		this.question = questionA[0];
		rest = rest.substring(questionA[0].length());
		//page
		String[] pageA = rest.trim().split(" ");
		//System.out.println(Arrays.toString(pageA));
		this.page = Integer.parseInt(pageA[0]);
		//System.out.println(rest);
	
		priority = 5;
		date = 0l;
	}
	
	public int compareTo(Entry other)
	{
		if(Resource.getRATIO() == 0){
			if(this.getDate() <= other.getDate()){
				return -1;
			}else {
				return 1;
			}
		}else{
			long unit = ( DateUtils.getLongDate() - System.currentTimeMillis() ) / (long) Resource.getRATIO();
			return (int) ( ( this.date - ( (long) (5-this.priority) * unit) ) - ( other.date  - ( (long) (5-other.priority) * unit) ) );
		}
		
		
	}
	public boolean equals(Object o){
		if(o != null && o.getClass().equals(this.getClass()) ){
			Entry e = (Entry) o;
			return ( e.page == page && e.type.equals(type) && e.section.equals(section) && e.question.equals(question) );
		}else{
			return false;
		}
	}
	
	public int hashCode(){
		return section.hashCode() + type.hashCode() + question.hashCode();
	}

}
